package xkp.lesson4;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class TestCallable {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new MyCallable();
        FutureTask<Integer> ft1 = new FutureTask<>(callable);
        FutureTask<Integer> ft2 = new FutureTask<>(callable);
        Thread thread1 = new Thread(ft1);
        Thread thread2 = new Thread(ft2);
        thread1.start();
        thread2.start();
        //ft.get()获取线程执行完后的返回值，会等待线程结束
        System.out.println(ft1.get());
        System.out.println(ft2.get());
        System.out.println("程序执行结束");
    }
}


class MyCallable implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        for (int i = 1; i <= 10; i++) {
            //Thread.currentThread()当前线程
            System.out.println(i + ":" + Thread.currentThread().getName());
            //希望线程执行慢一点 可以使线程休眠
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return 10;
    }
}